import { useGenshinEmo } from '@/stores/Emoji'

export async function emojiUtil(comment: string): Promise<string> {
  const emo = useGenshinEmo()
  let pattern = /\[([^\]]+?)\]/g
  let pattern1 = /\[([^\]]+?)\]/
  let result = comment.match(pattern)
  let res = comment
  await emo.getEmotion()
  if (result) {
    for (let i = 0; i < result.length; i++) {
      for (let x = 0; x < emo.emoData.length; x++) {
        for (let y = 0; y < emo.emoData[x].list.length; y++) {
          if ('[' + emo.emoData[x].list[y].name + ']' === result[i]) {
            let icon = emo.emoData[x].list[y].icon
            let name = emo.emoData[x].list[y].name
            res = res.replace(pattern1, '<img alt=' + name + ' class="comment-emoji" src="' + icon + '"/>')
            break
          }
        }
      }
    }
  }
  return res
}
